var startIndex = 0;
var pageSize = 5;
var currentPage = 1;
var pageCount = 0;
var key_word;
var key_ratio;
var key_ratio_where = "more";
var ratio_vaule;
var status = "";
var access_token = $.cookie('access_token');
var tenant_id = $.cookie('tenant_id');
var uid = $.cookie('uid');
var activityJsonArr = []; // 活动jsonArray
var file_url;
// var J_xactivity = false;

/*
 * window.onload = function(){ var activity_status =
 * document.getElementById("activity_status").getElementsByTagName("li"); var
 * ratio_where =
 * document.getElementById("ratio_where").getElementsByTagName("li"); var ratio =
 * document.getElementById("ratio").getElementsByTagName("li"); for(i=0;i<activity_status.length;i++){
 * activity_status[i].onclick = function(){ if(this.innerHTML=="启用"){ status=1;
 * }else if(this.innerHTML=="停用"){ status=0; } } }
 * 
 * for(i=0;i<ratio_where.length;i++){ ratio_where[i].onclick = function(){
 * if(this.innerHTML=="大于"){ key_ratio_where="more"; }else
 * if(this.innerHTML=="小于"){ key_ratio_where="less"; }else{
 * key_ratio_where="all"; } } } for(i=0;i<ratio.length;i++){ ratio[i].onclick =
 * function(){ } } activityList(); }
 */

// 获取活动列表
function activityList() {
	var url = path + "/api/activity/query_list";
	$.post(url, {
		"self_id" : startIndex,
		"current_page" : currentPage,
		"page_size" : pageSize,
		"tenant_id" : tenant_id,
		"uid" : uid,
		"access_token" : access_token,
		"key_word" : key_word,
		"key_ratio" : key_ratio,
		"key_ratio_where" : key_ratio_where,
		"status" : 1,
		"ratio_vaule" : ratio_vaule,
	}, function(data) {
		var datas;
		var msg;
		datas = JSON.parse(data);
		msg = datas.data;
		file_url = datas.file_url;
		activityhtml(msg);
		page(datas.totalCount);

	});
}
function activityhtml(data) {
	if (data == undefined) {
		return;
	}
	var html = "";
	for (var i = 0; i < data.length; i++) {
		var result = data[i];
		var receiveCouponNum = parseInt(result.receive_coupon)
				+ parseInt(result.twiceReceive);
		/*
		 * html += "<li>"; html+='<div class="chance">'; html+='<tr id="line'+result.id+'"><td><dl class="k_img_box">';
		 * html+='<dt><img src="'+path+'/images/k_img.png"/></dt>'; html+='<dd><h5><a
		 * href="'+path+'/api/bm/ui/activityDetail?activity_id='+result.id+'">'+result.name+'</a></h5>';
		 * html+='<div class="m_xstra">'; html+='<img
		 * src="'+path+'/images/star_1.png" />'; html+='<img
		 * src="'+path+'/images/star_1.png" />'; html+='<img
		 * src="'+path+'/images/star_1.png" />'; html+='<img
		 * src="'+path+'/images/star_1.png" />'; html += '<dfn>9.8分</dfn></div>';
		 * html += '<p>领取数/总数:' + result.receive_coupon + '</p>'; html += '<p>有效期至' +
		 * result.start_date + '</p></dd></dl></td></div>'; html +='<div
		 * class="tags"><span>+ result.tag_defined + </span></div>'; html +='<div
		 * class="tags main"><em>本次平均<br />转化率<b>20%</b></em><em>历史最高转化率<b>80%</b></em><em>历史最低转化绿<b>60%</b></em></div>';
		 * html += '<ol class="k_ol">'; html += '</ol></td>'; html +='<div
		 * class="tags nor"><span>+ (result.status == 1 ? "正常" : "停用")+</span></div>'
		 * html += "</li>";
		 */
		html += '<li activityId="' + result.id + '" name="' + result.name
				+ '" web_url="' + result.web_url + '">';
		html += '<div class="chance">';
		html += '<dl class="k_img_box">';
		html += '<dt><img style="width: 75px; height: 100px" src="' + file_url
				+ result.head_pic + '"></dt>';
		html += '<dd>';
		html += '<h5><div style="width:141px;height:17px;overflow:hidden;text-overflow:ellipsis;white-space:norwap;"><a href="'
				+ path
				+ '/api/bm/ui/activityDetail?activity_id='
				+ result.id
				+ '">' + result.name + '</a></div></h5>';
		html += '<div class="m_xstra" style="margin-top:-4px;height: 25px;">';
		html += '<img src="' + path + '/images/star_1.png">';
		html += '<img src="' + path + '/images/star_1.png">';
		html += '<img src="' + path + '/images/star_1.png">';
		html += '<img src="' + path + '/images/star_1.png">';
		html += '<img src="' + path + '/images/star_1.png">';
		html += '<dfn>9.8分</dfn>';
		html += '</div><br><br>';
		html += '<p>领取数:' + receiveCouponNum + '/总数:' + result.receive_coupon
				+ '</p>';
		if (StringEmpty(result.end_time)) {
			html += '<p>有效期至'
					+ (result.end_time == 'undefined' ? "" : result.end_time)
					+ '</p>';
		} else {
			html += '<p style="display:none">有效期至'
					+ (result.end_time == 'undefined' ? "" : result.end_time)
					+ '</p>';
		}
		html += '</dd>';
		html += '</dl>';
		html += '</div>';
		/*
		html += '<div class="tags">';
		html += '<span>' + result.tag_defined + '</span>';
		html += '</div>';
		*/
		html += '<div class="tags">';
		html += '<span>转化率:' + result.ratio + '%</span>';
		// html += '<span>转化率:'+result.ratio+'%</span>';
		html += '</div>';
		html += '<div class="tags nor">';
		html += '<span>' + (result.status == 1 ? "正常" : "停用") + '</span>';
		html += '</div>';
		html += '</li>';
	}
	$('#activity_list').html(html);
}
$(".m_xpages select").change(function() {
	pageSize = Number($(this).val());
	activityList();
});

$(".m_xpages a.prev").click(function() {
	if (currentPage > 1) {
		currentPage--;
		startIndex -= pageSize;
		activityList();
	}
});

$(".m_xpages a.next").click(function() {
	if (currentPage < pageCount) {
		currentPage++;
		startIndex += pageSize;
		activityList();
	}
});

$(".m_xpages :text").keyup(function(e) {
	if (e.keyCode == 13) {
		var pageGo = $(this).val();
		if (/^[0-9]+$/.test(pageGo)) {
			pageGo = Number(pageGo);
			if (pageGo < 1 || pageGo > pageCount) {
				alert('请输入合法页数');
			} else if (pageGo != currentPage) {
				startIndex = (pageGo - 1) * pageSize;
				activityList();
			}
		} else {
			alert('请输入合法页数');
		}
	}
});
function page(totalCount) {
	pageCount = totalCount / pageSize;
	if (totalCount % pageSize > 0)
		pageCount++;
	pageCount = parseInt(pageCount);
	currentPage = startIndex / pageSize + 1;
	currentPage = parseInt(currentPage);

	if (currentPage == 1) {
		$(".prev").addClass("pageDisable");
	} else {
		$(".prev").removeClass("pageDisable");
	}

	if (currentPage == pageCount || pageCount == 0) {
		$(".next").addClass("pageDisable");
	} else {
		$(".next").removeClass("pageDisable");
	}

	$(".m_xpages>.mian>span:eq(1)").text('共' + totalCount + '条');
	$(".m_xpages>.mian>span:eq(2)").text(currentPage + '/' + pageCount + '页');
}
// 点击取消按钮
$('.white,.J_xsutup').click(function() {
	$('.m_xpopups').hide();
	$('.J_xcontact , .J_xactivity').removeClass('show')
});
/* 点击更多 */
$(document).on("click", ".J_xseed", function() {
	activityList();
	$('.J_xactivity').show();
	// $('.m_xpropell').show();
});

$(function() {
	activityJsonArr = [];
	$(document).on("click", ".J_xactivity ul>li", function() {
		if ($(this).hasClass("on")) {
			$(this).removeClass("on");
			for (var i = 0; i < activityJsonArr.length; i++) {
				var json = activityJsonArr[i];
				if ($(this).attr("activityId") == json.activityId) {
					activityJsonArr.splice(i, 1);
					break;
				}
			}
		} else {
			$(this).addClass("on");
			var json = {
				'activityId' : $(this).attr("activityId"),
				'name' : $(this).attr("name"),
				'web_url' : $(this).attr("web_url")
			};
			activityJsonArr.push(json);
		}
	});
});

// 判断字符串是否为空
function StringEmpty(content) {
	if (content == undefined || content == "" || content == null) {
		return false;
	} else {
		return true;
	}
}

function childFinish() {
	$('.import').val("");
	var text = "";
	var mg = "";
	/*
	 * $(".J_xactivity ul>li[class='on']").each(function(i,d){ text +=
	 * $(this).find('div>dl>dd>h5>a').text()+"\r"; });
	 */
	for (var i = 0; i < activityJsonArr.length; i++) {
		var json = activityJsonArr[i];
		text += json.name + "(" + json.web_url + ")" + "\r";
	}
	for (var i = 0; i < msg.length; i++) {
		if (msg[i] != "") {
			mg += msg[i] + "\r";
		}
	}

	$('.import').val(mg + text);
	$('.m_xpopups').hide();
	$('.J_xcontact , .J_xactivity').removeClass('show');

	activityJsonArr = [];
}